package petrochina.company.electric.other;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 处理网格布局平分问题
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
    private int spacingVertical;
    private int spacingHorizontal;
    private int spanCount;

    public GridSpacingItemDecoration(int spacingVertical, int spacingHorizontal, int spanCount) {
        this.spacingVertical = spacingVertical;
        this.spacingHorizontal = spacingHorizontal;
        this.spanCount = spanCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        int column = position % spanCount;

        outRect.left = column * spacingHorizontal / spanCount;
        outRect.right = spacingHorizontal - (column + 1) * spacingHorizontal / spanCount;

        if (position < spanCount) {
            outRect.top = spacingVertical;
        }
        outRect.bottom = spacingVertical;
    }
}
